<?php
/**
 * Note: *
 * Date: 2021/7/12
 */
namespace App\Caches;

use Illuminate\Support\Str;

class QuickSetCache extends ShareCache
{
    const KEY_CLOSE_HOT_UPDATE = 'closeHotUpdate';
    const KEY_CLOSE_UPGRADE = 'closeUpgrade';
    const KEY_CLOSE_LOGIN = 'closeLogin';
    const KEY_CLOSE_REAL_NAME = 'closeRealName';

    /**
     * @param string $key
     * @param $value
     * @return bool
     */
    public static function set(string $key, $value)
    {
        return self::cache()->set($key, $value);
    }

    /**
     * @param string $key
     * @param null $default
     * @return array|mixed
     */
    public static function get(string $key, $default = null)
    {
        return self::cache()->get($key);
    }

    /**
     * @param string $key
     * @return bool
     */
    public static function del(string $key)
    {
        return self::cache()->forget($key);
    }

    /**
     * 通过反射获取所有预设常量
     * @return array
     */
    public static function all()
    {
        $objClass = new \ReflectionClass(__CLASS__);
        $allConsts = $objClass->getConstants();
        $all = [];
        foreach ($allConsts as $key => $val) {
            if (!Str::startsWith($key, 'KEY_')) continue;
            $all[$val] = self::get($val);
        }
        return $all;
    }

    /**
     * 批量设置
     * @param array $data
     * @return bool
     */
    public static function setAll(array $data)
    {
        $objClass = new \ReflectionClass(__CLASS__);
        $allConsts = $objClass->getConstants();

        foreach ($data as $key => $val) {
            if (array_search($key, $allConsts)) {
                self::set($key, $val);
            }
        }
        return true;
    }
}
